/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package objects;
import java.util.ArrayList;
import org.placelab.core.BeaconMeasurement;
import org.placelab.core.WiFiReading;
import org.placelab.spotter.Spotter;
import org.placelab.spotter.SpotterException;
import org.placelab.spotter.WiFiSpotter;
/**
 *
 * @author Nam
 */
public class WifiManager
{
    public WifiManager()
    {
        
    }
    

    public ArrayList<AccessPoint> startScanning() throws SpotterException
    {
        ArrayList<AccessPoint> m_APList = new ArrayList<AccessPoint>();
        //WifiSpotter Example : The WiFiSpotter scans all of the 802.11 wireless cards on the machine and returns the networks that it sees.
            Spotter s = new WiFiSpotter(10000);
            try {
                s.open();
                BeaconMeasurement m_BeaconMeasurement = (BeaconMeasurement) s.getMeasurement();
                //System.out.println(m_BeaconMeasurement.numberOfReadings() + " APs were seen\n");
                if (m_BeaconMeasurement.numberOfReadings() > 0) {
                    //System.out.println(pad("MAC Address", 20) + pad("SSID", 30)+ pad("RSSI", 10));
                    // Iterate through the Vector and print the readings
                    for (int i = 0; i < m_BeaconMeasurement.numberOfReadings(); i++)
                    {
                        WiFiReading m_wifiReading = (WiFiReading) m_BeaconMeasurement.getReading(i);
                        //System.out.println(pad(m_wifiReading.getId(), 20)+ pad(m_wifiReading.getSsid(), 30) + pad("" + m_wifiReading.getRssi(), 10));

                        //create an array that stores the scanning result
                        if (m_wifiReading.getRssi()>-80)
                        {
                            AccessPoint m_AP = new AccessPoint(m_wifiReading);
                            m_APList.add(m_AP);
                        }
                    }
                }
                s.close();
            } catch (SpotterException ex) {
                ex.printStackTrace();
            }
            
            return m_APList;
    }

    public static String pad(String str, int len)
    {
        StringBuffer sb = new StringBuffer(str);
        for (int i=str.length(); i < len; i++)
        {
            sb.append(" ");
        }
        return sb.toString();
    }
}
